package com.ntschy.web.controller.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.common.annotation.OperationDataScope;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import com.ntschy.common.core.entity.dataobject.SysUser;
import com.ntschy.sys.entity.dto.user.SysUserAddUpdateDTO;
import com.ntschy.sys.entity.dto.user.SysUserPageDTO;
import com.ntschy.sys.entity.mapstruct.SysUserMapstruct;
import com.ntschy.sys.entity.valid.SysUserGroups;
import com.ntschy.sys.mapper.SysUserMapper;
import com.ntschy.sys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * user controller
 * @author ezreal_geng
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户接口")
@Validated
public class SysUserController extends BaseController<SysUser> {

    @Autowired
    private ISysUserService userService;

    // * ================================================基础增删改查BEGIN============================================= *

    @ApiOperation(value = "查询用户列表", notes = "")
    @ApiImplicitParam(name = "queryDto", value = "用户列表查询实体类", required = true, dataType = "SysUserPageDTO")
    //@PreAuthorize("@ss.hasPermi('sys:user:page')")
    @PostMapping("/pageUser")
    public AjaxPageResult<SysUser> pageUser(@RequestBody SysUserPageDTO pageDTO) {
        Page<SysUser> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        SysUser queryUser = SysUserMapstruct.SYS_USER_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<SysUser> list = userService.pageUser(queryUser);
        return buildPageResult(list, page);
    }

    @ApiOperation(value = "新增用户", notes = "")
    @ApiImplicitParam(name = "addDTO", value = "新增用户实体类", required = true, dataType = "SysUserAddDTO")
    //@PreAuthorize("@ss.hasPermi('sys:user:add')")
    @PostMapping("/addUser")
    public AjaxResult<String> addUser(@Validated(SysUserGroups.Add.class) @RequestBody SysUserAddUpdateDTO addDTO) {
        SysUser addUser = SysUserMapstruct.SYS_USER_MAP_STRUCT.addUpdateDtoToDo(addDTO);
        return toAjax(userService.addUser(addUser), "新增成功", "新增失败");
    }

    @ApiOperation(value = "更新用户", notes = "")
    @ApiImplicitParam(name = "updateDTO", value = "更新用户实体类", required = true, dataType = "SysUserAddDTO")
    //@PreAuthorize("@ss.hasPermi('sys:user:update')")
    @PostMapping("/updateUser")
    //@OperationDataScope(filedPk = "ID",  dataScopeFiledName = "CreateUserID", propPk = "id", mapperClass = SysUserMapper.class)
    public AjaxResult<String> updateUser(@Validated(SysUserGroups.Update.class) @RequestBody SysUserAddUpdateDTO updateDTO) {
        SysUser updateUser = SysUserMapstruct.SYS_USER_MAP_STRUCT.addUpdateDtoToDo(updateDTO);
        return toAjax(userService.updateUser(updateUser), "更新成功", "更新失败");
    }

    @ApiOperation(value = "删除用户", notes = "")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:user:delete')")
    @GetMapping("/deleteUser")
    @OperationDataScope(filedPk = "ID",  dataScopeFiledName = "CreateUserID", propPk = "userId", mapperClass = SysUserMapper.class)
    public AjaxResult<String> deleteUser(@NotBlank(message = "用户ID不能为空") @Length(min = 36, max = 36, message = "用户ID长度不合法") String userId) {
        return toAjax(userService.deleteUser(userId), "删除成功", "删除失败");
    }

    // * =================================================基础增删改查END============================================== *
}
